package com.ask.threadcase5;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread extends Thread {
    static int ticket = 0;
    static Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) { //线程不断尝试获取cpu资源
            lock.lock();

            try {
                if (ticket == 100) { // 当任务结束
                    break;
                }
                else {
                    sleep(100);
                    ticket++;
                    System.out.println(getName()+"@"+ticket);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }

    }
}
